package com.anxin.registerManagement.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.anxin.registerManagement.bean.jo.ClassRoomSearchJo;
import com.anxin.registerManagement.bean.persist.ClassRoom;
import com.anxin.registerManagement.bean.persist.Profession;
import com.anxin.registerManagement.mapper.ClassRoomMapper;
import com.anxin.registerManagement.mapper.ProfessionMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 叶前呈
 * @date 2022/1/30 14:49
 */
@Service
public class ClassRoomService {

    private static Logger logger = LoggerFactory.getLogger(ClassRoomService.class);

    @Autowired
    ClassRoomMapper classRoomMapper;

    @Autowired
    ProfessionMapper professionMapper;

    /**
     * 查询班级
     */
    public PageInfo<ClassRoom> getClassRoomList(ClassRoomSearchJo classRoomSearchJo) {
        // 这里，如果没有传pageNum和pageSize则查询的是所有数据！
        if (!ObjectUtil.isEmpty(classRoomSearchJo.getPageNum()) && !ObjectUtil.isEmpty(classRoomSearchJo.getPageSize())) {
            PageHelper.startPage(classRoomSearchJo.getPageNum(), classRoomSearchJo.getPageSize());
        }
        // FIXME:这里可能有点丑，可以使用xml来进行封装，这里是XML封装
        return new PageInfo<>(classRoomMapper.getClassRoomList(classRoomSearchJo));
    }

    /**
     * 添加班级
     */
    public void addClassRoom(ClassRoom classRoom) {
        // FIXME:这里可能需要去除年级，

        // TODO:若9月1日之前，设立年份为今年，若9月1日之后，设立年份为明年，
        // TODO:这里需要判断班级是否存在，不存在，则班级编号为1，存在，则自动计算编号数量，然后加1
        // TODO:需要判断班主任是否存在，专业是否存在等
        classRoomMapper.insert(classRoom);
    }

    /**
     * 修改班级
     */
    public void updateClassRoom(ClassRoom classRoom) {
        // TODO:若9月1日之前，只能修改今年的班级，若9月1日之后，则修改明年的班级
        // TODO:这里写的简单了点，应该用wrapper进行设置，只能修改专业、班主任
        classRoomMapper.updateById(classRoom);

    }

    /**
     * 删除班级
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteClassRoom(List<Integer> id) {
        classRoomMapper.deleteBatchIds(id);
    }

    /**
     * 获取所有班级
     */
    public List<ClassRoom> getAllClassRoom() {
        return classRoomMapper.selectList(null);
    }

    /**
     * 获取所有班级详情
     */
    public List<JSONObject> getAllClassRoomDetail() {
        List<ClassRoom> classRooms = classRoomMapper.selectList(null);
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (ClassRoom classRoom : classRooms) {
            JSONObject jsonObject = new JSONObject();
            StringBuilder stringBuilder = new StringBuilder();
            Profession profession = professionMapper.selectById(classRoom.getProfessionId());
            stringBuilder.append(profession.getProfessionName()).append(classRoom.getClassYear().toString().substring(classRoom.getClassYear().toString().length() - 2)).append(classRoom.getClassCode());
            jsonObject.put("label", stringBuilder.toString());
            jsonObject.put("value",classRoom.getId());
            jsonObjectList.add(jsonObject);
        }
        return jsonObjectList;
    }
}
